$$ = $$ || {};
if (!window.$$) var $$ = {};
$$.app = $$.app || {};
if (!window.config) var config = {};

(function(app, j) {

	app.baseUrl = null;
    app.xhr = null;

    app.scrollTo = function (el, offeset) {

        var pos = el ? el.offset().top : 0;
        j('html,body').animate({
            scrollTop: pos + (offeset ? offeset : 0)
    	}, 'slow');
    };

    app.makeAjaxReq = function(url, params, eID, method, dataType, showLoading, onSuccess, onError, onComplete) {

        showLoading = (typeof(showLoading) === 'undefined' || showLoading === '') ? true : showLoading;
        method = (typeof(method) == 'undefined' || method == '' || (method.toUpperCase() != 'POST' && method.toUpperCase() != 'GET')) ? 'GET' : method.toUpperCase();
        dataType = (typeof(dataType) == 'undefined' || dataType == '') ? 'html' : dataType;

        if (typeof(onSuccess) == 'undefined' || onSuccess == '')
        {
            var _onSucess = function(data)
            {
                if (dataType.toLocaleLowerCase() == 'json')
                {
                    j(eID).html(data.form);
                }
                else
                {
                    j(eID).html(data);
                }
            };
        }
        else
        {
            var _onSucess = onSuccess;
        }

        if (typeof(onError) == 'undefined' || onError == '')
        {
            var _onError = function(jXHR, textStatus, errorThrown) {
                try {
                    j(eID).html("Sorry. There was an error.");
                }
                catch (e) {
                    alert("Sorry. There was an error.");
                }
            };
        }
        else
        {
            var _onError = onError;
        }

        if (typeof(onComplete) == 'undefined' || onComplete == '')
        {
            var _onComplete = function(jXHR, textStatus) {

            };
        }
        else
        {
            var _onComplete = onComplete;
        }

        if (showLoading)
        {
        	//TODO: When ajax loading
        }

        app.xhr = j.ajax({
            type    : method,
            url     : url,
            dataType: dataType,
            data    : params,
            success : _onSucess,
            error   : _onError,
            complete: _onComplete
        });

    };

    app.initFormValidate = function(form, rule, message, errorElement, errorPlacement) {

        if(!errorElement) errorElement = 'span';

        if(!errorPlacement)
            errorPlacement = function(error, element) {
                error.addClass('error');
                error.insertAfter(element);
            };

        return form.validate({
            ignore: ":hidden, .notValidate",
            errorElement: errorElement, //default input error message container
            focusInvalid: false, // do not focus the last invalid input
            rules: rule,
            debug: true,
            messages: message,
            invalidHandler: function (event, validator) { //display error alert on form submit
                var errors = validator.numberOfInvalids();
                if (errors) {
                    validator.errorList[0].element.focus();
                }
                app.scrollTo(form, -200);
            },
            errorPlacement: errorPlacement
        });
    };

})($$.app, $);